package cm.unit2.theme3;

import java.util.LinkedHashMap;
import java.util.Map;

public class FindDuplicateInArray {

	/**
	 * 	利用hash表的解法	
     * 
     * @param numbers int整型一维数组 
     * @return int整型
     */
    public static int duplicate (int[] numbers) {
        // write code here
        if (numbers == null || numbers.length == 0){
            return -1;
        }
        Map<Integer, Integer> map = new LinkedHashMap();
        int i = 0;
        for(;i < numbers.length;i++){
            if(map.get(numbers[i]) == null){
                map.put(numbers[i],1);
            }else{
                return numbers[i];
            }
        }
        return -1;
    }
    
	public static void main(String[] args) {
		 int []array01 = {2,3,1,0,2,5,3};
		 int []array02 = {};
		 int []array03 = null;
		 
		 int r1 = FindDuplicateInArray.duplicate(array01);
		 int r2 = FindDuplicateInArray.duplicate(array02);
		 int r3 = FindDuplicateInArray.duplicate(array03);
		 System.out.println(r1);
		 System.out.println(r2);
		 System.out.println(r3);
	}

}
